<template>
  <div style="background-color: #f5f6f7; width: 958px">
    <div class="base-owner-edit-owner-frame" >
      <el-form :model="editForm" :rules="rules" ref="editForm"  class="base-owner-edit-owner-inputForm">
        <el-form-item label="主体名称" prop="ownerName" class="el-form-item">
          <el-input v-model="editForm.ownerName" placeholder="输入主体名称" maxlength="20" clearable></el-input>
        </el-form-item>
        <el-form-item label="类别" prop="ownerType" class="el-form-item-right">
          <el-select class="base-owner-edit-owner-selector" v-model="editForm.ownerType" clearable filterable placeholder="选择类别">
            <el-option v-for="item in ownerTypeOption" :key="item.value" :label="item.label" :value="item.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="注册资本（元）" prop="registCapital" class="el-form-item">
          <el-input v-model="editForm.registCapital" placeholder="输入注册资本" maxlength="14" @keyup.native="verifyDecimal(editForm, 'registCapital')" clearable></el-input>
        </el-form-item>
        <el-form-item label="年营业额（元）" prop="annualTurnover" class="el-form-item-right">
          <el-input v-model="editForm.annualTurnover" placeholder="输入年营业额" maxlength="14" @keyup.native="verifyDecimal(editForm, 'annualTurnover')" clearable></el-input>
        </el-form-item>
        <el-form-item label="法人" prop="legalPerson" class="el-form-item">
          <el-input v-model="editForm.legalPerson" placeholder="输入法人" maxlength="10" clearable></el-input>
        </el-form-item>
        <el-form-item label="历史交易额（元）" prop="totalTradeAmount" class="el-form-item-right">
          <el-input v-model="editForm.totalTradeAmount" placeholder="输入历史交易金额" maxlength="14" @keyup.native="verifyDecimal(editForm, 'totalTradeAmount')" clearable disabled></el-input>
        </el-form-item>
        <el-form-item label="历史交易量（吨）" prop="totalTradeVolume" class="el-form-item">
          <el-input v-model="editForm.totalTradeVolume" placeholder="输入历史交易量" maxlength="14" @keyup.native="verifyDecimal(editForm, 'totalTradeVolume')" clearable disabled></el-input>
        </el-form-item>
        <el-form-item label="历史交易次数" prop="totalTradeTime" class="el-form-item-right">
          <el-input v-model="editForm.totalTradeTime" placeholder="输入历史交易次数" maxlength="6" @keyup.native="verifyInteger(editForm, 'totalTradeTime')" clearable disabled></el-input>
        </el-form-item>
        <el-form-item label="历史违规次数" prop="totalViolateTime" class="el-form-item">
          <el-input v-model="editForm.totalViolateTime" placeholder="输入历史违规次数" maxlength="6" @keyup.native="verifyInteger(editForm, 'totalViolateTime')" clearable disabled></el-input>
        </el-form-item>
        <el-form-item label="MIG评级" prop="migGrade" class="el-form-item-right">
          <el-select class="base-owner-edit-owner-selector" v-model="editForm.migGrade" clearable filterable placeholder="选择MIG评级" disabled>
            <el-option v-for="item in migGradeOption" :key="item.value" :label="item.label" :value="item.value"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
    </div>
    <input-com :headTitle="'MIG提供参考资料'" :inputValue="this.editForm.migReference" :headSonTitle="'选填'" @getInputVal="getInputReferenceVal" :isActive="true"></input-com>
    <el-row class="base-owner-edit-owner-btnFrame">
      <el-button class="base-owner-edit-owner-btn" @click="submitForm('editForm')" type="primary">保存</el-button>
    </el-row>
  </div>
</template>
<script>
  import maValidate from 'utils/common-method/validate/index';
  import inputCom from 'components/input/baseTitleInput.vue';
  import {validateTips} from 'utils/tips';
  import PcDialog from 'components/dialog';
  export default {
    components: {
      inputCom,
      PcDialog
    },
    props: ['ownerInfo'],
    data() {
      var checkAmount = (rule, value, callback) => {
        if (!value) {
          return callback();
        }
        if (value) {
          setTimeout(() => {
            if (parseFloat(value) > 99999999999.99) {
              callback(new Error('输入值超出限制，可支持整数11位、小数2位\n'));
            } else {
              callback();
            }
          }, 500);
        }
      };
      return {
        initForm: JSON.stringify(this.ownerInfo),
        editForm: JSON.parse(JSON.stringify(this.ownerInfo)),
        rules: {
          ownerName: [
            {required: true, message: validateTips.FILL_BASE_INFO, trigger: 'blur'},
            {min: 0, max: 20, message: validateTips.LESS_20, trigger: 'blur'}
          ],
          ownerType: [
            {required: true, message: validateTips.FILL_BASE_INFO, trigger: 'blur'}
          ],
          registCapital: [
            {required: true, message: validateTips.FILL_BASE_INFO, trigger: 'blur'},
            {validator: checkAmount, trigger: 'blur'}
          ],
          annualTurnover: [
            {required: true, message: validateTips.FILL_BASE_INFO, trigger: 'blur'},
            {validator: checkAmount, trigger: 'blur'}
          ],
          legalPerson: [
            {required: true, message: validateTips.FILL_BASE_INFO, trigger: 'blur'},
            {min: 0, max: 10, message: validateTips.LESS_10, trigger: 'blur'}
          ]
        },
        ownerTypeOption: [
          {
            value: 1,
            label: '贸易企业'
          }, {
            value: 2,
            label: '经销企业'
          }, {
            value: 3,
            label: '终端企业'
          }
        ],
        migGradeOption: [
          {
            value: 'AAA',
            label: 'AAA'
          }, {
            value: 'AA',
            label: 'AA'
          }, {
            value: 'A',
            label: 'A'
          }, {
            value: 'BBB',
            label: 'BBB'
          }, {
            value: 'BB',
            label: 'BB'
          }, {
            value: 'B',
            label: 'B'
          }, {
            value: 'C',
            label: 'C'
          }
        ]
      };
    },
    created() {
      this.editForm = JSON.parse(JSON.stringify(this.ownerInfo));
    },
    methods: {
      // 校验整数
      verifyInteger(val, prop) {
        maValidate.verifyInteger(val, prop);
      },
      // 校验小数
      verifyDecimal(val, prop) {
        maValidate.verifyDecimal(val, prop);
      },
      // 获取MIG提供参考资料的输入内容
      getInputReferenceVal(val) {
        this.editForm.migReference = val;
      },
      // 点击提交按钮
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            this.commitForm();
          } else {
            return false;
          }
        });
      },
      // 请求后端接口
      commitForm() {
        // 内容检查：判断提交前后有无内容变化
        if (this.initForm === JSON.stringify(this.editForm)) {
          this.errorTip('内容没有任何变化，请确认');
          return;
        }
        var that = this;
        this.ajax({
          type: 'POST',
          url: '/malisk/server/base/saveOwner',
          data: {
            id: that.editForm.id,
            version: that.editForm.version,
            ownerName: that.editForm.ownerName,
            ownerType: that.editForm.ownerType,
            registCapital: that.editForm.registCapital,
            annualTurnover: that.editForm.annualTurnover,
            legalPerson: that.editForm.legalPerson,
            initTradeAmount: that.editForm.initTradeAmount,
            initTradeVolume: that.editForm.initTradeVolume,
            initTradeTime: that.editForm.initTradeTime,
            initViolateTime: that.editForm.initViolateTime,
            migGrade: that.editForm.migGrade,
            migReference: that.editForm.migReference
          },
          loading: true,
          success: (res) => {
            setTimeout(() => {
              this.$pcNProgress.done();
            }, 300);
            if (res.code === 200) {
              this.successTip('保存成功', 2000);
              that.$emit('sure');
            } else {
              this.errorTip(res.message);
            }
          }
        });
      }
    }
  };
</script>
<style>
  .base-owner-edit-owner-frame {
    display: flex;
    flex-wrap: wrap;
    width: 958px;
    height: 430px;
    border: 1px solid #e5e5e5;
    background-color: white
  }
  .base-owner-edit-owner-frame .el-form-item {
    display: inline-block;
    width: 420px;
    margin-left: 20px;
    margin-right: 20px;
  }
  .base-owner-edit-owner-frame .el-form-item-right {
    float: right;
  }
  .base-owner-edit-owner-inputForm {
    margin-top:10px;
  }
  .base-owner-edit-owner-btnFrame {
    width: 100%;
    margin-top: 40px;
    margin-bottom: 20px;
    text-align: center;
  }
  .base-owner-edit-owner-btn {
    width: 140px;
    height: 40px;
  }
  .base-owner-edit-owner-selector {
    width: 420px;
  }
</style>
